package cn.iocoder.yudao.module.promotion.dal.mysql.channel;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.promotion.controller.admin.channel.vo.ChannelLinkExportReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.channel.vo.ChannelLinkPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.channel.vo.ChannelLinkReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.channel.ChannelLinkDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 商城链接配置 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface ChannelLinkMapper extends BaseMapperX<ChannelLinkDO> {

    default PageResult<ChannelLinkDO> selectPage(ChannelLinkPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ChannelLinkDO>()
                .likeIfPresent(ChannelLinkDO::getName, reqVO.getName())
                .eqIfPresent(ChannelLinkDO::getDomain, reqVO.getDomain())
                .eqIfPresent(ChannelLinkDO::getUrl, reqVO.getUrl())
                .eqIfPresent(ChannelLinkDO::getPoints, reqVO.getPoints())
                .eqIfPresent(ChannelLinkDO::getEconomizePrice, reqVO.getEconomizePrice())
                .eqIfPresent(ChannelLinkDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ChannelLinkDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ChannelLinkDO::getId));
    }

    default List<ChannelLinkDO> selectList(ChannelLinkExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ChannelLinkDO>()
                .likeIfPresent(ChannelLinkDO::getName, reqVO.getName())
                .eqIfPresent(ChannelLinkDO::getDomain, reqVO.getDomain())
                .eqIfPresent(ChannelLinkDO::getUrl, reqVO.getUrl())
                .eqIfPresent(ChannelLinkDO::getPoints, reqVO.getPoints())
                .eqIfPresent(ChannelLinkDO::getEconomizePrice, reqVO.getEconomizePrice())
                .eqIfPresent(ChannelLinkDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ChannelLinkDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ChannelLinkDO::getId));
    }
    default ChannelLinkDO getDetail(ChannelLinkReqVO linkVO) {
        return selectOne(new LambdaQueryWrapperX<ChannelLinkDO>()
                .eq(ChannelLinkDO::getUrl, linkVO.getUrl()));
    }

    default PageResult<ChannelLinkDO> selectStatisticsPage(ChannelLinkPageReqVO pageReqVO) {
        // 创建分页对象
        Page<ChannelLinkDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        // 查询分页结果
        IPage<ChannelLinkDO> iPage = selectPage(page, pageReqVO);
        // 返回封装的PageResult
        return new PageResult<>(iPage.getRecords(), iPage.getTotal());
    }
    @Select("<script>" +
            "SELECT zcl.*,( SELECT count(*) FROM zfkj_interview_log zil WHERE zil.url_id = zcl.id ) AS visit_number,\n" +
            "( SELECT sum( too.pay_price ) FROM trade_order too WHERE too.interview_log_id = zcl.id AND too.`status` > 0 and too.`status` &lt; 40 )*0.01 AS pay_price,\n" +
            "( SELECT sum( too.pay_price ) FROM trade_order too WHERE too.interview_log_id = zcl.id AND too.`status` in (0,40) )*0.01 AS not_pay_price\n" +
            "FROM\n" +
            "\tzfkj_channel_link zcl where zcl.deleted = 0" +
            "</script>")
    IPage<ChannelLinkDO> selectPage(Page<ChannelLinkDO> pageParam, ChannelLinkPageReqVO pageReqVO);
}
